using System;
using System.DHTML;
using Ext;
using Ext.data;
using Ext.util;

namespace SampleScripts.view
{
	internal delegate Photo PrepareDataHandler(Photo data);
	public class Photo
	{
		public string name;
		public string size;
		public string shortName;
		public string sizeString;
		public string dateString;
		public DateTime lastmod;
	}

	//internal delegate void LabelEditorMouseDownHandler(EventObject e, DOMElement target);
	//internal delegate void LabelEditorSaveHandler(Editor e, string value);
	//internal delegate void EditorInitHandler(View view);
	//public class LabelEditor : Editor
	//{
	//    static LabelEditor()
	//    {
	//        // HACK
	//        Script.Eval("SampleScripts.view.LabelEditor.prototype.init = function(view) { this.initLabelEditor(view); };");
	//    }

	//    public LabelEditor(object config) : base(config)
	//    {
	//        this.alignment = "tl-tl";
	//        this.hideEl = false;
	//        this.cls = "x-small-editor";
	//        Type.SetField(this, "shim", false);
	//        this.completeOnEnter = true;
	//        this.cancelOnEsc = true;
	//        Type.SetField(this, "labelSelector", "span.x-editable");
	//    }

	//    public void initLabelEditor(View view)
	//    {
	//        Type.SetField(this, "view", view);
	//        view.on("render", new AnonymousDelegate(initEditor), false);
	//        view.on("complete", new LabelEditorSaveHandler(onSave), false);
	//    }

	//    private void initEditor()
	//    {
	//        View view = (View)Type.GetField(this, "view");
	//        view.getEl().on("mousedown", new LabelEditorMouseDownHandler(onMouseDown), this, null);
	//    }

	//    private void onMouseDown(EventObject e, DOMElement target)
	//    {
	//        if (((bool)e.ctrlKey) == false && ((bool)e.shiftKey) == false)
	//        {
	//            View view = (View)Type.GetField(this, "view");
	//            DOMElement item = view.findItemFromChild(target);
	//            e.stopEvent();

	//            int i = (int)Type.InvokeMethod(view, "indexOf", item);
	//            Store store = (Store)Type.GetField(view, "store");
	//            Record record = store.getAt(i);

	//            int idx = (int)Type.GetField(this, "dataIndex");
	//            startEdit(target, (string)record.get(idx));
	//        }
	//        else
	//        {
	//            e.preventDefault();
	//        }
	//    }

	//    private void onSave(Editor ed, string value)
	//    {
	//        Record rec = (Record)Type.GetField(this, "activeRecord");
	//        int idx = (int)Type.GetField(this, "dataIndex");
	//        Type.InvokeMethod(rec, "set", idx, value);
	//    }
	//}


	public class DataViewScript
	{
		public static void main(Dictionary args)
		{
			ExtClass.onReady(new AnonymousDelegate(delegate { new DataViewScript().init(); }));
		}

		public void init()
		{
			Store store = new JsonStore(new JsonStoreConfig()
             	.url("get-images.ashx/Get")
             	.custom("root", "images")
             	.fields(new object[] {
						new Dictionary("name", "url"),
						new Dictionary("name", "size", "type", "float"),
						new Dictionary("name", "lastmod", "type", "date", "dateFormat", "timestamp")})
			.ToDictionary());
			store.load();

			XTemplate tpl = new XTemplate(
				@"<tpl for=""."">
					<div class=""thumb-wrap"" id=""{name}"">
					<div class=""thumb""><img src=""{url}"" title=""{name}""></div>
					<span class=""x-editable"">{shortName}</span></div>
				</tpl>
				<div class=""x-clear""></div>");

			Panel panel = new Panel(new PanelConfig()
            	.id("images-view")
            	.frame(true)
            	.width(535)
            	.autoHeight(true)
            	.collapsible(true)
            	.layout("fit")
            	.title("Simple DataView with editable labels, multi and drag selection")
            	.items(new DataView(new DataViewConfig()
            		.store(store)
            		.tpl(tpl)
            		.autoHeight(true)
            		.multiSelect(true)
            		.overClass("x-view-over")
            		.itemSelector("div.thumb-wrap")
            		.emptyText("No images to display")
					//.plugins(new object[]
					//            {
					//                new DataView.DragSelector(),
					//                new LabelEditor(new Dictionary("dataIndex", "name"))
					//            })
					.custom("prepareData",
							new PrepareDataHandler(delegate(Photo data)
								{
									data.shortName = Format.ellipsis(data.name, 15);
									data.sizeString = Format.fileSize(data.size);
									data.dateString = data.lastmod.ToString();
									return data;
								}))
            		.ToDictionary()))
	            .ToDictionary());
			panel.render(Document.Body);
		}
	}
}
